#ifndef POSTEDEVENT_H
#define POSTEDEVENT_H

#include <QThread>
#include <QApplication>
#include <QObject>
#include <QDebug>

/******************************************************************************
 *
 * @file       PostedEvent.h
 * @brief      生成事件
 *
 * @author     xiYan
 * @date       2025/08/23
 * @history
 *****************************************************************************/


class Worker : public QThread
{
    Q_OBJECT
public:
    void run() override
    {
        qDebug() << "worker running " << QThread::currentThread();
        sleep(1);
        // 这里new event 由QT释放
        // postEvent支持跨线程，而sendEvent是在同一下线程
        QApplication::postEvent(receiverObj,new QEvent(QEvent::User));

        // 这里调用说明postEvent 不会阻塞是异步调用
        qDebug() << "postEvent asyn call ";
    }
public:
    QObject* receiverObj;
};

class Receiver : public QObject
{
    Q_OBJECT
protected:
    bool event(QEvent *event) override
    {
        if(event->type() == QEvent::User)
        {
            qDebug() << "receiver in main " << QThread::currentThread();
            // 退出应用
            QApplication::quit();
            return true;
        }
        return QObject::event(event);
    }
};


#endif // POSTEDEVENT_H
